package cn.zjh.system.controller;

import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.PageList;
import cn.zjh.system.domain.Dictionarytype;
import cn.zjh.system.query.DictionarytypeQuery;
import cn.zjh.system.service.IDictionarydetailService;
import cn.zjh.system.service.IDictionarytypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dictionarytype")
@RestController
public class DictionarytypeController {
    @Autowired
    private IDictionarytypeService iDictionarytypeService;
    @Autowired
    private IDictionarydetailService iDictionarydetailService;

    @GetMapping
    public List<Dictionarytype> findAll(){
       return iDictionarytypeService.findAll();
    }

    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id") Long id){
        return iDictionarytypeService.findOne(id);
    }

    @PostMapping
    public PageList<Dictionarytype> queryData(@RequestBody DictionarytypeQuery dictionarytypeQuery){

        return iDictionarytypeService.queryPage(dictionarytypeQuery);
    }

    @DeleteMapping("/{id}")
    public AjaxResult deletee(@PathVariable("id ") Long id) {
        try {
            iDictionarytypeService.deletee(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype ){
        try {
            if(dictionarytype.getId() == null){
                iDictionarytypeService.save(dictionarytype);
            }else {
                iDictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false,"操作成功");
        }
    }


    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            iDictionarytypeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

}
